package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.Counts;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.CountsService;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersControler {

    @Autowired
    private UserInfoService userInfoService;




    /**
     * 查询用户资料
     */
    @GetMapping
    public ResponseEntity users(Long userID) {
        if(userID == null) {
            userID = UserHolder.getUserId();
        }
        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo) {
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    @Autowired
    private CountsService countsService;

    /**
     * 统计互相喜欢，喜欢，粉丝的数量
     * @param
     * @return
     */
    @GetMapping("/counts")
    public ResponseEntity counts() {
        Counts counts = this.countsService.Counts();
        return ResponseEntity.ok(counts);
    }

    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     * @param page
     * @param pagesize
     * @param type
     * @param nickname
     * @return
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity friendInteraction(@RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "10") Integer pagesize,
                                            @PathVariable Integer type,
                                            String nickname){
        PageResult pageResult = userInfoService.friendInteraction(1,pagesize,type,nickname);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 粉丝喜欢，即添加当前粉丝到喜欢表中，并建立双向好友关系
     * @param uid 粉丝ID
     * @return
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity fanLike(@PathVariable Long uid){
        userInfoService.fanLike(uid);
        return ResponseEntity.ok(null);
    }

    @DeleteMapping("/like/{uid}")
    public ResponseEntity cancelLike(@PathVariable Long uid){
        userInfoService.cancelLike(uid);
        return ResponseEntity.ok(null);
    }


    /**
     * 更新用户头像
     * @param headPhoto
     * @return
     * @throws IOException
     */
    @PostMapping("/header")
    public ResponseEntity updateHeader( MultipartFile headPhoto) throws IOException {
        Long id = UserHolder.getUserId();
        userInfoService.updateHead(headPhoto,id);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号，发送验证码
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        userInfoService.sendVerificationCode();
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号，校验验证码
     * @param map
     * @return
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){
        String verificationCode = (String) map.get("verificationCode");
        Map restMap = userInfoService.checkVerificationCode(verificationCode);
        return ResponseEntity.ok(restMap);
    }

    /**
     * 修改手机号
     * @param map
     * @return
     */
    @PostMapping("phone")
    public ResponseEntity saveNewPhone(@RequestBody Map map){
        String newPhone = (String) map.get("phone");
        userInfoService.saveNewPhone(newPhone);
        return ResponseEntity.ok(null);
    }




}
